package com.example.mybatisplusdemo.tools;

import com.example.mybatisplusdemo.model.domain.Flight;
import com.example.mybatisplusdemo.model.dto.SmartCombinationDTO;

import java.time.Duration;
import java.util.List;

public class FlightCombinationUtils {

    public static void populateCombinationData(SmartCombinationDTO dto, List<Flight> flights) {
        dto.setTotalPrice(calculateTotalPrice(flights));
        dto.setTotalDuration(calculateTotalDuration(flights));
        dto.setAdvantages(detectAdvantages(flights));
        dto.setRecommendationScore(calculateRecommendationScore(flights));
    }

    private static Double calculateTotalPrice(List<Flight> flights) {
        return flights.stream()
                .mapToDouble(f -> f.getPrice().doubleValue())
                .sum();
    }

    private static Integer calculateTotalDuration(List<Flight> flights) {
        if (flights == null || flights.isEmpty()) return 0;

        long flightMinutes = flights.stream()
                .mapToLong(f -> Duration.between(
                        f.getDepartureTime(),
                        f.getArrivalTime()
                ).toMinutes())
                .sum();

        long transferMinutes = 0;
        if (flights.size() > 1) {
            for (int i = 0; i < flights.size() - 1; i++) {
                transferMinutes += Duration.between(
                        flights.get(i).getArrivalTime(),
                        flights.get(i + 1).getDepartureTime()
                ).toMinutes();
            }
        }

        return (int)(flightMinutes + transferMinutes);
    }

    private static String detectAdvantages(List<Flight> flights) {
        StringBuilder advantages = new StringBuilder();

        // 同航空公司优势
        if (flights.stream().map(Flight::getAirlineCode).distinct().count() == 1) {
            advantages.append("同航司中转|行李直挂|");
        }

        // 中转时间优势
        if (flights.size() > 1) {
            long transferTime = Duration.between(
                    flights.get(0).getArrivalTime(),
                    flights.get(1).getDepartureTime()
            ).toMinutes();

            if (transferTime >= 90 && transferTime <= 180) {
                advantages.append("理想中转时间|");
            }
        }

        // 价格优势
        double avgPrice = calculateTotalPrice(flights) / flights.size();
        if (avgPrice < 1000) {
            advantages.append("超值组合|");
        }

        return advantages.length() > 0 ?
                advantages.substring(0, advantages.length() - 1) :
                "标准组合";
    }

    private static Double calculateRecommendationScore(List<Flight> flights) {
        double priceScore = 1 - (calculateTotalPrice(flights) / 10000);
        double punctualityScore = flights.stream()
                .mapToDouble(f -> f.getPunctualityRate().doubleValue())
                .average()
                .orElse(0);
        double comfortScore = flights.stream()
                .mapToDouble(f -> f.getSeatComfort().doubleValue())
                .average()
                .orElse(0);

        return priceScore * 0.4 + punctualityScore * 0.3 + comfortScore * 0.3;
    }
}
